package com.xique.park.controller.client.web.service.domain.service.impl;

import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkIn;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.bean.domain.ParkOut;
import com.xique.park.controller.client.web.service.domain.service.IWxAppClientDomainService;
import com.xique.park.service.service.IParkInService;
import com.xique.park.service.service.IParkOnService;
import com.xique.park.service.service.IParkOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author shd
 * @create 2021/11/10 15:00
 */
@Service
public class WxAppClientDomainServiceImpl implements IWxAppClientDomainService {

    @Autowired
    private IParkInService parkInService;

    @Autowired
    private IParkOutService parkOutService;

    @Autowired
    private IParkOnService parkOnService;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean changeCarType(ParkIn updateParkIn, ParkOut updateParkOut) {

        if (StringUtils.isNotNull(updateParkIn)) {
            int count = parkInService.updateParkIn(updateParkIn);
            if (count <= 0) {
                return false;
            }
        }

        if (StringUtils.isNotNull(updateParkOut)) {
            int count = parkOutService.updateParkOut(updateParkOut);
            if (count <= 0) {
                return false;
            }
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean inLanePass(ParkIn parkIn, ParkOn parkOn) {

        if (StringUtils.isNotNull(parkIn)) {
            int count = parkInService.updateParkIn(parkIn);
            if (count <= 0) {
                return false;
            }
        }

        if (StringUtils.isNotNull(parkOn)) {
            int count = parkOnService.updateParkOn(parkOn);
            if (count <= 0) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean inLaneCancel(ParkIn parkIn, Long parkOnId) {

        if (StringUtils.isNotNull(parkIn)) {
            int count = parkInService.updateParkIn(parkIn);
            if (count <= 0) {
                return false;
            }
        }

        if (StringUtils.isNotNull(parkOnId)) {
            int count = parkOnService.deleteParkOnById(parkOnId);
            if (count <= 0) {
                return false;
            }
        }

        return true;
    }
}
